import { createStore as _createStore } from 'vuex'
import { useCookies } from '@vueuse/integrations/useCookies'
import adminLayout from "~/layouts/admin.vue"
import { removeToken, setToken } from '~/composables/auth.js'
import { getUSerInfo } from '~/api/xm.js'
const store = _createStore({
    state() {
        return {
            // 侧边宽度
            asideWidth: "250px",
            realname: undefined,
            // 多页标签getInfo
            tagList: [{
                title: "后台首页",
                path: "/"
            }],
            user: {},
            menus: []
        }
    },
    getters: {
        // total(state) {
        //     let t = 0
        //     state.cartList.forEach(v => {
        //         if (v.checked) {
        //             t += (v.count * v.price)
        //         }
        //     })
        //     return t
        // }
    },
    mutations: {
        addCart(state, cart) {
            const item = state.cartList.find(v => {
                return v.id == cart.id
            })
            if (item) {
                item.count++
            } else {
                cart.count = 1
                state.cartList.push(cart)
            }
        },
        changeCount(state, obj) {
            const item = state.cartList.find(v => {
                return v.id == obj.id
            })
            item.count = obj.count
        },
        SET_ROLE(state, r) {
            state.realname = r
        },
        SET_USER(state, user) {
            state.user = user
        },
        SET_STUDENTID(state, r) {
            state.studentId = r
        },
        SET_NAME(state, r) {
            state.name = r
        },
        SET_BANJIID(state, r) {
            state.banjiId = r
        },
        // 展开/缩起侧边
        handleAsideMenu(state) {
            state.asideWidth = state.asideWidth == "250px" ? "64px" : "250px"
        },
        // 添加标签页
        addTag(state, { path, title }) {
            let i = state.tagList.findIndex(o => o.path == path)
            if (i == -1) {
                state.tagList.push({
                    path,
                    title
                })

                // 存储cookie
                const cookies = useCookies(['tagList'])
                cookies.set("tagList", state.tagList)
            }

        },
        // 初始化tagList
        initTagList(state) {
            // 获取cookie
            const cookies = useCookies(['tagList'])
            const tagList = cookies.get("tagList")
            if (tagList) {
                state.tagList = tagList
            }
        },
        // 删除tag
        removeTag(state, path) {
            let i = state.tagList.findIndex(o => o.path == path)
            if (i != -1) {
                state.tagList.splice(i, 1)

                // 存储cookie
                const cookies = useCookies(['tagList'])
                cookies.set("tagList", state.tagList)
            }
        },
        // 关闭其他标签
        clearOtherTag(state, path) {
            state.tagList = state.tagList.filter(o => o.path == "/" || o.path == path)

            // 存储cookie
            const cookies = useCookies(['tagList'])
            cookies.set("tagList", state.tagList)
        },
        // 清除所有标签
        clearAllTag(state) {
            state.tagList = [{
                title: "后台首页",
                path: "/"
            }]

            // 存储cookie
            const cookies = useCookies(['tagList'])
            cookies.set("tagList", state.tagList)
        },

        // 记录用户信息
        SET_USERINFO(state, user) {
            state.user = user
        },

        // 记录菜单
        SET_MENUS(state, menus) {
            state.menus = menus
        },
        // 记录权限
        SET_RULENAMES(state, ruleNames) {
            state.ruleNames = ruleNames
        },
    },
    actions: {
        login({ commit }, form) {
            console.log(form);
            return new Promise((resolve, reject) => {
                login(form).then(res => {
                    console.log(res);
                    // 存储token
                    setToken(res.token)
                    resolve()
                }).catch(err => reject(err))
            })
        },
        getInfo({ commit }, token) {
            console.log('================', token);
            return new Promise((resolve, reject) => {
                getUSerInfo().then(res => {
                    console.log("getInfo", res);
                    // commit('SET_MENUS', res.menutree)
                    // commit('SET_ROLE', res.realname)
                    commit('SET_USER', res.data)
                    resolve(res.data)
                })

                // commit('SET_MENUS', [{
                //     name: '学生',
                //     children: [{
                //             path: '/xuesheng/chakan',
                //             name: '学生查看'
                //         },
                //         {
                //             path: '/xuesheng/shenqing',
                //             name: '学生申请'
                //         }
                //     ]
                // }])


            })
        },
        logout({ commit }) {
            return new Promise((resolve, reject) => {
                // 清除token
                removeToken()
                commit('SET_USERINFO', {})
                commit('SET_MENUS', [])
                commit('SET_RULENAMES', [])
                commit("clearAllTag")
                resolve()
            })
        },
    }
})

export default store